import { AuthService } from './auth.service'
import { Body, Controller, Get, Post, Query, Headers, Req } from '@nestjs/common'
import { AllowNoToken, AuthEnum, LoginAccountDTO, TokenPayload } from '@/common'

@Controller()
export class AuthController {
  constructor(private readonly authService: AuthService) {}

  /** 获取图片验证码 */
  @AllowNoToken()
  @Get('captcha')
  public getCaptcha() {
    return this.authService.getCaptcha()
  }

  /** 登录 */
  @AllowNoToken()
  @Post('login')
  public login(@Req() request: ExpressRequest, @Body() body: LoginAccountDTO) {
    return this.authService.login(request, body)
  }

  /** 获取登录账号的信息 */
  @Get('getInfo')
  public getInfo(@TokenPayload('userId') userId: string) {
    return this.authService.getInfo(userId)
  }

  /** 获取登录账号的路由表信息 */
  @Get('getRoutes')
  public getRoutes(@TokenPayload('userId') userId: string) {
    return this.authService.getRoutes(userId)
  }

  /** 退出登录 */
  @AllowNoToken()
  @Post('logout')
  public logout(@Headers(AuthEnum.AUTHORIZATION) authorization: string) {
    if (!authorization) return '退出成功'
    const token = authorization.split(' ')[1]
    return this.authService.logout(token)
  }
}
